//
//  DataBaseSingleton.m
//  豆瓣
//
//  Created by dllo on 15/12/15.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "DataBaseSingleton.h"
#import <sqlite3.h>


@implementation DataBaseSingleton

+ (instancetype)shareDataBase{
    static DataBaseSingleton *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DataBaseSingleton alloc] init];
    }
    return dataBase;
}

static sqlite3 *db = nil;

- (void)openDB{
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [documentPath stringByAppendingPathComponent:@"douban.sqlite"];
    NSLog(@"filePath = %@", filePath);
    
    // 打开数据库
    BOOL result = sqlite3_open(filePath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据库成功");
    }else {
        NSLog(@"打开数据库失败");
    }
}

 /////////////////////////////////////////////////////////

// 创建电影收藏数据库表
- (void)createTable{
    // 创建SQL语句
    // number声明成了主键, 系统自动生成键值
    // mycollection:表名
    NSString *sql = @"CREATE TABLE IF NOT EXISTS mycollection(number INTEGER PRIMARY KEY AUTOINCREMENT, movieName TEXT, movieId TEXT, pic_url TEXT)";
    // 执行sql语句
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }else {
        NSLog(@"创建失败");
    }
}

// 插入一个电影收藏
- (void)insertMovieIntoTable:(Movie *)movie{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO mycollection(movieName,movieId,pic_url) VALUES('%@','%@','%@')", movie.movieName, movie.movieId, movie.pic_url];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}


// 查找
- (NSMutableArray *)selectMovie{
    NSMutableArray *array = [NSMutableArray array];
    // 查询所有
    NSString *sql = @"SELECT * FROM mycollection";
    sqlite3_stmt *stmt = nil;
    // 准备查询工作
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else {
        NSLog(@"准备失败");
        return nil;
    }
    
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *movieName = sqlite3_column_text(stmt, 1);
        const unsigned char *movieId = sqlite3_column_text(stmt, 2);
        const unsigned char *pic_url = sqlite3_column_text(stmt, 3);
        
        NSString *name = [NSString stringWithUTF8String:(const char *)movieName];
        NSString *Id = [NSString stringWithUTF8String:(const char *) movieId];
        NSString *pic = [NSString stringWithUTF8String:(const char *)pic_url];
        Movie *movie = [[Movie alloc] init];
        movie.movieName = name;
        movie.movieId = Id;
        movie.pic_url = pic;
        
        [array addObject:movie];
    }
    
    
    return array;
}
// 删除我的电影收藏中的数据
- (void)deleteTable:(NSString *)name{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM mycollection WHERE movieName = '%@'", name];
    char *err = "";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, &err);
    
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
        NSLog(@"%s", err);
    }
}

/////////////////////////////////////////////////////

// 创建活动数据库表
- (void)createActivityTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS myActivity(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT)";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }else {
        NSLog(@"创建失败");
    }
}

// 插入一个活动收藏
- (void)insertActivityIntoTable:(ActivityList *)activity{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO myActivity(title) VALUES('%@')", activity.title];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
    
}

- (NSMutableArray *)selectActivity{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = @"SELECT *FROM myActivity";
    sqlite3_stmt *stmt = nil;
    
    BOOL result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else {
        NSLog(@"准备失败");
        return nil;
    }
    
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        
        NSString *name = [NSString stringWithUTF8String:(const char *)title];
        ActivityList *activity = [[ActivityList alloc] init];
        activity.title = name;
        
        [array addObject:activity];
    }
    
    return array;
}


// 删除我的活动收藏中的数据
- (void)deleteActivityTable:(NSString *)title{
    title = [title stringByReplacingOccurrencesOfString:@"\'" withString:@"‘"];
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM myActivity WHERE title = '%@'", title];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}

//////////////////////////////////////////////////////////////
// 创建活动数据库表
- (void)createActivityDetailTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS activityDetail(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, begin_time TEXT, end_time TEXT, category_name TEXT, address TEXT, wisher_count TEXT, participant_count TEXT, image_hlarge TEXT, content TEXT)";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    }else {
        NSLog(@"创建失败");
    }
    
}

// 插入一个活动数据
- (void)insertActivityDetailIntoTable:(ActivityList *)activity{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO activityDetail(title, begin_time, end_time, category_name, address, wisher_count, participant_count, image_hlarge, content) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", activity.title, activity.begin_time, activity.end_time, activity.category_name, activity.address, activity.wisher_count, activity.participant_count, activity.image_hlarge, activity.content];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
    
}

- (NSMutableArray *)selectActivityDetail{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = @"SELECT *FROM activityDetail";
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else {
        NSLog(@"准备失败");
        return nil;
    }
    
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        const unsigned char *begin_time = sqlite3_column_text(stmt, 2);
        const unsigned char *end_time = sqlite3_column_text(stmt, 3);
        const unsigned char *category_name = sqlite3_column_text(stmt, 4);
        const unsigned char *address = sqlite3_column_text(stmt, 5);
        const unsigned char *wisher_count = sqlite3_column_text(stmt, 6);
        const unsigned char *participant_count = sqlite3_column_text(stmt, 7);
        const unsigned char *image_hlarge = sqlite3_column_text(stmt, 8);
        const unsigned char *content = sqlite3_column_text(stmt, 9);
        
        
        
        NSString *name = [NSString stringWithUTF8String:(const char *)title];
        NSString *begin = [NSString stringWithUTF8String:(const char *)begin_time];
        NSString *end = [NSString stringWithUTF8String:(const char *)end_time];
        NSString *category = [NSString stringWithUTF8String:(const char *)category_name];
        NSString *add = [NSString stringWithUTF8String:(const char *)address];
        NSString *wisher = [NSString stringWithUTF8String:(const char *)wisher_count];
        NSString *participant = [NSString stringWithUTF8String:(const char *)participant_count];
        NSString *image = [NSString stringWithUTF8String:(const char *)image_hlarge];
        NSString *conte = [NSString stringWithUTF8String:(const char *)content];
        
        ActivityList *activity = [[ActivityList alloc] init];
        activity.title = name;
        activity.begin_time = begin;
        activity.end_time = end;
        activity.category_name = category;
        activity.address = add;
        activity.wisher_count = @([wisher integerValue]);
        activity.participant_count = @([participant integerValue]);
        activity.image_hlarge = image;
        activity.content = conte;
        
        [array addObject:activity];
    }
    
    return array;
    
}

// 删除真个表
- (void)dropActivityDetailTable{
    
    NSString *sql = @"DROP TABLE activityDetail";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除表成功");
    }else {
        NSLog(@"删除表失败");
    }
}

    





@end
